/*
 * Copyright (c) Extreme Vision Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#ifndef COMMON_DET_INFER_H
#define COMMON_DET_INFER_H
#include <memory>
#include <map>
#include <mutex>
#include <opencv2/core/mat.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/dnn.hpp>
#include <unistd.h>


#include <migraphx/program.hpp>
#include <migraphx/onnx.hpp>
#include <migraphx/gpu/target.hpp>
#include <migraphx/quantization.hpp>

#include "ji_utils.h"

typedef struct BoxInfo
{
    float x1;
    float y1;
    float x2;
    float y2;
    float score;
    int label;
} BoxInfo;




class SampleDetector
{
public:
    SampleDetector();
    ~SampleDetector();
    bool Init(float thresh, const std::string &uuid);
    bool ProcessImage(const cv::Mat &img, std::vector<BoxInfo> &det_results);
    bool UnInit();

private:
    void runNms(std::vector<BoxInfo> &vecBoxObjs);
    std::vector<BoxInfo> BatchNMS(std::vector<BoxInfo> &vecBoxObjs);
    void decode_outputs(float *buffer, std::vector<BoxInfo> &objects, float scale, const int img_w, const int img_h);

private:
    migraphx::program m_net;
    cv::Size m_inputSize;
    std::string m_inputName;
    std::string m_outputName;
    migraphx::shape m_inputShape;
    migraphx::shape m_outputShape;
    std::vector<cv::Mat> m_chw_wrappers{};
    cv::Mat m_board;
    cv::Mat m_normalized_mat;
    bool mInitialized = false;
    float mThresh = 0.3;

    std::string m_uuid;
};

#endif
